package com.zzy.emo;

import java.net.MalformedURLException;
import java.net.URL;

public class URLUtil {
	
	public static String getBaseUrl(String url) {
		URL u = null;
		try {
			u = new URL(url);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		String host = u.getHost();
		String protocol = u.getProtocol();
		
		String baseUrl = protocol+ "://"+ host;
		
		return baseUrl;
	}
	
	
	public static String getPath(String url) {
		URL u = null;
		try {
			u = new URL(url);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		String path = u.getPath();
		return path;
	}
	
	public static String getParam(String url) {
		URL u = null;
		try {
			u = new URL(url);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		String path = u.getQuery();
		return path;
	}
	
	
	
	public static void main(String[] args) {
		String url = "http://baidu.com/cns/sss.html?id=123";
		System.out.println(URLUtil.getBaseUrl(url));
		System.out.println(URLUtil.getPath(url));
		System.out.println(URLUtil.getParam(url));
	}

}
